<template>
  <div class="app-container">
    <div class="card-box">
      <el-space>
        <!-- <el-input v-model="params.search" placeholder="输入关键字" />
        <el-button type="primary" @click="shippingTemplateStore.fetchShippingTemplateList(params)">
          搜索
        </el-button> -->
        <el-button type="primary" @click="handleToAdd">添加模板</el-button>
      </el-space>
    </div>
    <div class="card-box">
      <el-table v-loading="shippingTemplateStore.loading.list" :data="shippingTemplateStore.list">
        <el-table-column label="ID" prop="id" />
        <el-table-column label="模板名称" prop="name" />
        <el-table-column label="计费方式">
          <template #default="{ row }">
            <el-tag v-if="(row as ShippingTemplateType).type == 'quantity'" type="primary">
              按件数计费
            </el-tag>
            <el-tag v-if="(row as ShippingTemplateType).type === 'volume'" type="warning">
              按体积计费
            </el-tag>
            <el-tag v-if="(row as ShippingTemplateType).type == 'weight'" type="danger">
              按重量计费
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="指定包邮">
          <template #default="{ row }">
            <el-tag v-if="(row as ShippingTemplateType).enable_free" type="primary">是</el-tag>
            <el-tag v-else type="danger">否</el-tag>
          </template>
        </el-table-column>

        <el-table-column label="排序" prop="sort" />
        <el-table-column label="添加时间" prop="created_at" />
        <el-table-column label="操作">
          <template #default="scope">
            <!-- <el-button type="primary" link>编辑</el-button> -->
            <el-popconfirm title="确定删除吗？" placement="top" @confirm="handleDelete(scope.row)">
              <template #reference>
                <el-button type="danger" link>删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        v-if="shippingTemplateStore.list.length > 0"
        v-model:current-page="params.page"
        v-model:page-size="params.size"
        background
        style="margin-top: 10px"
        :total="shippingTemplateStore.totalCount"
      />
    </div>
    <Form v-model="visible" :type="formType" @confirm="handleConfirm" />
  </div>
</template>

<script setup lang="ts">
import { ShippingTemplateListParams, ShippingTemplateType } from "@/api/goods/shipping-template";
import { useShippingTemplateStoreHook } from "@/store/modules/goods/shipping-template";
import Form, { FormType } from "./components/form.vue";

const shippingTemplateStore = useShippingTemplateStoreHook();

const params = reactive<ShippingTemplateListParams>({
  search: "",
  page: 1,
  size: 10,
});

const visible = ref(false);

const formType = ref<FormType>("add");

function handleToAdd() {
  formType.value = "add";
  visible.value = true;
}

async function handleDelete(row: ShippingTemplateType) {
  await shippingTemplateStore.fetchDeleteShippingTemplate(row.id);
  await shippingTemplateStore.fetchShippingTemplateList(params);
}

function handleConfirm() {
  shippingTemplateStore.fetchShippingTemplateList(params);
}

onMounted(() => {
  shippingTemplateStore.fetchShippingTemplateList(params);
});
</script>

<style lang="scss" scoped></style>
